/*
 * list.c
 *
 *  Created on: Jun 14, 2010
 *      Author: Guilherme
 */

#include "list.h"

/* This method receives a pointer to a list and adds a new node to it. */
LinkedList append_node(LinkedList n, int nodeID, int linkCost) {
	LinkedList new;

	new = malloc(sizeof(struct node));
	if (new == NULL) {
		fprintf(stderr, "Could not allocate a new node. Not enough space!");
		exit(2);
	}
	new->nodeID = nodeID;
	new->linkCost = linkCost;
	if (n != NULL) {
		new->next = n->next;
		n->next = new;
	} else {
		new->next = NULL;
		n = new;
	}

	return n;
}

LinkedList remove_node(LinkedList n) {
	LinkedList tmp;


	tmp = n->next;
	if (tmp != NULL)
		n->next = tmp->next;

	return tmp;
}

/* This method receives a pointer to a list and removes all items from it. */
LinkedList empty(LinkedList n) {
	LinkedList p;

	while (n != NULL) {
		p = n;
		n = n->next;
		free(p);
	}
	return n;
}

/* This method receives a pointer to a list and prints all items from it. */
void print_list(LinkedList n) {

	if (n == NULL) {
		fprintf(stdout, "This is an empty list! Nothing to print.");
	}

	while (n != NULL) {
		fprintf(stdout, "Node ID = %d \n", n->nodeID);
		n = n->next;
	}
}

